local fk__zhuanzheng = fk.CreateSkill {

  name = "fk__zhuanzheng",

  tags = {  },

}

local U = require "packages/utility/utility"


fk__zhuanzheng:addEffect(fk.AfterCardsMove, {

  anim_type = "support",

  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(fk__zhuanzheng.name) or player.hp < 1 then return end
    for _, move in ipairs(data) do
      if move.from and move.from:isKongcheng() then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            return true
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.from and move.from:isKongcheng() then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            table.insertIfNeed(targets, move.from)
            break
          end
        end
      end
    end
    --room:sortPlayersByAction(targets)
    for _, target_id in ipairs(targets) do
      if not player:hasSkill(fk__zhuanzheng.name) or player.hp < 1 then break end
      local skill_target = target_id
      if skill_target and not skill_target.dead then
        self:doCost(event, skill_target, player, data)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, fk__zhuanzheng.name, data, "#fk__zhuanzheng-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    player.room:doIndicate(player.id, {target.id})
    player.room:loseHp(player, 1, fk__zhuanzheng.name)
    local x = target.maxHp - target:getHandcardNum()
    if not target.dead and x > 0 then
      target:drawCards(math.min(x, 5), fk__zhuanzheng.name)
    end
  end,
})

return fk__zhuanzheng